/**
 * Created by Administrator on 2017/9/22 0022.
 */

var dispatcher;
if (!dispatcher) {
    dispatcher = {};
}

dispatcher.state = function (value, row, index) {
    var state = row.disState;
    if (state == 2) {
        return [
            '<div><a class="status" href="javascript:void(0)" style="color: #0bb20c">启用</a></div>'
        ].join('');
    } else if (state == 1) {
        return [
            '<a class="state" href="javascript:void(0)" style="color: red">禁用</a>'
        ].join('');
    }
};

dispatcher.certification = function (value, row, index) {
    var isCertificate = row.isCertificate;
    if (isCertificate == 1) {
        return [
            '<div>是</div>'
        ].join('');
    } else {
        return [
            '<div>否</div>'
        ].join('');
    }
};

window.operate = {
    'click .state': function (e, value, row) {
        comment.showOperationTips("确定要禁用配送人员吗？", function () {
            $.post($().AbsUrlParse('/web/dispatcher/updateState'), {id: row.id, disState: 2}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("设置成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .status': function (e, value, row) {
        comment.showOperationTips("确定要启用配送人员吗？", function () {
            $.post($().AbsUrlParse('/web/dispatcher/updateState'), {id: row.id, disState: 1}, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("设置成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

dispatcher.operateFormatter = function (value, row, index) {
    if (row.handheldPhotos == "" || row.fontCard == "" || row.reverseCrad === "") {
        return [
            '<a class="dispatcherDetails" href="javascript:void(0)" >认证详情</a>'
        ].join('');
    } else {
        return [
            '<a class="dispatcherDetails" href="javascript:void(0)" >认证详情</a>' +
            '&nbsp;&nbsp;' +
            '<a class="through" href="javascript:void(0)" style="color: #00ca6d">认证通过</a>'
        ].join('');
    }
};

window.operateEvent = {
    'click .dispatcherDetails': function (e, value, row) {
        showModel("queryDispatcherPic-div", function () {
            var url = File_Download_Url;
            var img = url + row.handheldPhotos;
            var img1 = url + row.fontCard;
            var img2 = url + row.reverseCrad;
            $("#photo").attr("src", img);
            $("#card").attr("src", img1);
            $("#photoCon").attr("src", img2);
        });

    },
    'click .order': function (e, value, row) {
        showModel("queryDispatcherOrder-div", function () {
            var table = $("#orderDispatcher");
            var url = $().AbsUrlParse("/web/dispatcher/dispatcherOrder?id=" + row.id);
            table.bootstrapTable("refresh", {url: url});
            orderState = function (value, row, index) {
                if (row.order_state == 1) {
                    return [
                        '<div><span>待付款</span></div>'
                    ].join('');
                } else if (row.order_state == 2) {
                    return [
                        '<div><span>待接单</span></div>'
                    ].join('');
                } else if (row.order_state == 3) {
                    return [
                        '<div><span>待配送</span></div>'
                    ].join('');
                } else if (row.order_state == 4) {
                    return [
                        '<div><span>配送中</span></div>'
                    ].join('');
                } else if (row.order_state == 5) {
                    return [
                        '<div><span>待评价</span></div>'
                    ].join('');
                } else if (row.order_state == 6) {
                    return [
                        '<div><span>已完结</span></div>'
                    ].join('');
                } else if (row.order_state == 7) {
                    return [
                        '<div><span>申请退款</span></div>'
                    ].join('');
                } else if (row.order_state == 8) {
                    return [
                        '<div><span>拒绝接单</span></div>'
                    ].join('');
                } else if (row.order_state == 9) {
                    return [
                        '<div><span>同意退款</span></div>'
                    ].join('');
                } else if (row.order_state == 10) {
                    return [
                        '<div><span>拒绝退款</span></div>'
                    ].join('');
                } else if (row.order_state == 11) {
                    return [
                        '<div><span>用户取消</span></div>'
                    ].join('');
                } else if (row.order_state == 12) {
                    return [
                        '<div><span>待取货</span></div>'
                    ].join('');
                }
            };
            payType = function (value, row, index) {
                if (row.pay_type == 1) {
                    return [
                        '<div><span>支付宝</span></div>'
                    ].join('');
                } else if (row.pay_type == 2) {
                    return [
                        '<div><span>微信</span></div>'
                    ].join('');
                }
            }
        });
    },
    'click .details': function (e, value, row) {
        showModel("queryDispatcherDetails-div", function () {
            var table = $("#detailsDispatcher");
            var url = $().AbsUrlParse("/web/dispatcher/dispatcherOrderDetails?id=" + row.id);
            table.bootstrapTable("refresh", {url: url});
        });
    },
    'click .through': function (e, value, row) {
        comment.showOperationTips("确定通过认证吗？", function () {
            $.post($().AbsUrlParse('/web/dispatcher/throughDispatcher'), {
                id: row.id, isCertificate: 1
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("认证成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    },
    'click .resetPassword': function (e, value, row) {
        showModel("resetPasswordDispatcher-div", function () {
            $("#ids").val(row.id);
            $("#resetPassword").val(123456);
        });
    },
    'click .remove': function (e, value, row) {
        comment.showOperationTips("确定删除吗？", function () {
            $.post($().AbsUrlParse('/web/dispatcher/updateState'), {
                id: row.id, disState: 0
            }, function (data) {
                if (data.state == true) {
                    if (data.res.code == 40000) {
                        hint("删除成功");
                        var table = $("#table");
                        table.bootstrapTable("refresh");
                    } else {
                        hint(data.res.msg);
                    }
                } else {
                    hint(data.msg);
                }
            });
        });
    }
};

/**
 *执行修改保存操作
 * */
bindClick("button[data-event=submit-updateDispatcher]", function () {
    var imagePath = $("#headPic").attr("img-path");
    $("#pic").val(imagePath);

    //省份
    var cityId = $("#getAllCitys").val();
    $("#provincesIdUpdate").val(cityId);

    //城市
    var citys = $("#getJuniorCitys").val();
    $("#cityHierarchyUpdate").val(citys);

    //区域
    var city = $("#getRegionCitys").val();
    $("#regionIdUpdate").val(city);

    var form = $("#updateDispatcherFrom");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse("/web/dispatcher/updateDispatcherInfo"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('修改成功！');
            //关闭model方法
            closeModel('updateDispatcher-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

/**
 *执行修改保存操作
 * */
bindClick("button[data-event=submit-resetPassword]", function () {

    var form = $("#resetPasswordFrom");
    var v = form.valid();
    if (v == false) {
        return false;
    }
    var params = form.formToArray();
    $.post($().AbsUrlParse("/web/dispatcher/resetPassword"), params, function (res) {
        if (res.state && res.res.code == 40000) {
            hint('重置成功！');
            //关闭model方法
            closeModel('resetPasswordDispatcher-div');
            var table = $("#table");
            table.bootstrapTable("refresh");
        } else {
            if (res.state == false) {
                hint(res.msg);
            } else {
                hint(res.res.msg);
            }
        }
    });
});

function queryNoDispatcher() {
    var name = $("#nameDis").val();
    var created = $("#created").val();
    var obj = created.split("-");
    var startCreate = obj[0] + "-" + obj[1] + "-" + obj[2];
    if (obj[1] == undefined) {
        startCreate = "";
    }
    var endCreate = obj[3] + "-" + obj[4] + "-" + obj[5];
    if (obj[4] == undefined) {
        endCreate = "";
    }

    var lastTime = $("#loginTime").val();
    var obj1 = lastTime.split("-");
    var startTime = obj1[0] + "-" + obj1[1] + "-" + obj1[2];
    if (obj1[1] == undefined) {
        startTime = "";
    }
    var endTime = obj1[3] + "-" + obj1[4] + "-" + obj1[5];
    if (obj1[4] == undefined) {
        endTime = "";
    }
    var cityId = $("#citys").val();
    var url = $().AbsUrlParse("/web/dispatcher/queryNoAuthentication?name=" + name + "&startCreate=" + startCreate + "&endCreate=" + endCreate + "&cityId=" + cityId + "&startTime=" + startTime + "&endTime=" + endTime);
    var table = $("#table");
    table.bootstrapTable("refresh", {url: url});
}

dispatcher.status = function (value, row, index) {
    if (row.disState == 1) {
        return [
            '<div><span>启用</span></div>'
        ].join('');
    } else if (row.disState == 2) {
        return [
            '<div><span>禁用</span></div>'
        ].join('');
    }
};


//省份
var inquiryCityInfo = function () {
    $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 2}, function (id) {
        var select = $("#getAllCitys");
        select.empty();
        select.append('<option value="0">-请选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
            }
        }
    });
};
inquiryCityInfo();


//城市
var inquiryJunior = function () {
    var city = $("#getAllCitys").val();
    if (city == 0) {
        hint("请选择省份");
        return;
    }
    if (city == 0) {
        $("#show").hide();
    } else {
        $("#show").show();
    }
    var assignment = $("#assignment").val();
    $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 3, cityId: city}, function (id) {
        var select = $("#getJuniorCitys");
        select.empty();
        select.append('<option value="0">-请选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
                $("#getJuniorCitys").val(assignment);
            }
        }
    });
};

var inquiryCity = function () {
    var city = $("#getJuniorCitys").val();
    if (city == 0) {
        hint("请选择城市");
        return;
    }
    if (city == 0) {
        $("#show1").hide();
    } else {
        $("#show1").show();
    }
    var withs = $("#with").val();
    $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 4, cityId: city}, function (id) {
        var select = $("#getRegionCitys");
        select.empty();
        select.append('<option value="0">-请选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
                $("#getRegionCitys").val(withs);
            }
        }
    });
};


var getDisCity = function () {
    $.post($().AbsUrlParse("/web/type/queryByCityId"), {deptPath: 3}, function (id) {
        var select = $("#citys");
        select.empty();
        select.append('<option value="0">-城市选择-</option>');
        if (id.res.code == 40000) {
            var allData = id.res.data;
            for (var i = 0; i < allData.length; i++) {
                select.append('<option value="' + allData[i].id + '">' + allData[i].name + '</option>');
            }
        }
    });
};
getDisCity();


function initDateRange() {
    $(".rangedate").daterangepicker({
        "autoApply": true,
        "locale": {
            "applyLabel": "确定",
            "cancelLabel": "取消",
            "fromLabel": "开始",
            "toLabel": "结束",
            "customRangeLabel": "Custom",
            "weekLabel": "周",
            "daysOfWeek": [
                "日",
                "一",
                "二",
                "三",
                "四",
                "五",
                "六"
            ],
            "monthNames": [
                "1月",
                "2月",
                "3月",
                "4月",
                "5月",
                "6月",
                "7月",
                "8月",
                "9月",
                "10月",
                "11月",
                "12月"
            ],
            "cancelLabel": '清空',
            "opens": "right"
        },
        "format": "YYYY-MM-DD"
    }).on('cancel.daterangepicker', function (ev, picker) {
        $(this).val('');
    });
}

//初始化页面的时候运行该函数
$(function () {
    initDateRange();
});

function enlargeImage(value) {
    showModel("detailImage-div", function () {
        var imgs = '<image src=' + value.src + ' style="height:500px" />';
        $("#image").html(imgs);
    })
}